package cn.shujuhai.common.service.impl;

import cn.shujuhai.common.domain.Setting;
import cn.shujuhai.common.mapper.SettingMapper;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.SettingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author myh
 */
@RequiredArgsConstructor
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting>
        implements SettingService {

    final CacheManager cacheManager;
    private final SettingMapper settingMapper;

    public void initGlobalParamsToRedis() {
        // 获取sys_setting缓存
        Cache cache = cacheManager.getCache("sys_setting");
        // 查询所有全局参数
        List<Setting> list = this.list();
        for (Setting setting : list) {
            // 将全局参数添加到缓存中
            assert cache != null;
            cache.put(setting.getSettingKey(), setting.getSettingValue());
        }
    }

    @Override
    public List<Setting> getList() {

        return this.list();
    }

    @Override
    public boolean insertSelective(Setting setting) {

        boolean a = this.save(setting);
        if (a) {
            initGlobalParamsToRedis();
        }
        return a;
    }

    @Override
    public int delById(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public boolean updateSelective(Setting setting) {
        boolean b = this.updateById(setting);
        if (b) {
            initGlobalParamsToRedis();
        }
        return b;
    }

    @Override
    public Setting selectById(Long settingId) {
        return this.baseMapper.selectById(settingId);
    }

    @Override
    public PageInfo<Setting> getPagedSettingsByName(String name, int pageNum, int pageSize) {
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<Setting> settings = this.baseMapper.selectList(new QueryWrapper<Setting>().like("setting_name", name));
        // 返回分页结果
        return new PageInfo<>(settings);
    }

    @Override
    public int batchdelete(List<Long> ids) {
        return this.baseMapper.deleteByIds(ids);
    }


    /**
     * 获取全局参数中的密钥
     */
    @Override
    public String getSecretKey() {
        Setting setting = settingMapper.selectOne(new QueryWrapper<Setting>().eq("setting_key", "SECRET_KEY"));
        if (setting != null) {
            return setting.getSettingValue();
        }
        throw new RuntimeException("无法找到全局参数中的系统密钥");
    }

    /**
     * 获取全局参数中的初始向量
     */
    @Override
    public String getInitVector() {
        Setting setting = settingMapper.selectOne(new QueryWrapper<Setting>().eq("setting_key", "INIT_VECTOR"));
        if (setting != null) {
            return setting.getSettingValue();
        }
        throw new RuntimeException("无法找到全局参数中的初始向量");
    }

    /**
     * 获取全局参数中的版权信息
     */
    @Override
    public String getWebCopyright() {
        Setting setting = settingMapper.selectOne(new QueryWrapper<Setting>().eq("setting_key", "WEB_COPYRIGHT"));
        if (setting != null) {
            return setting.getSettingValue();
        }
        throw new RuntimeException("无法找到全局参数中的版权信息");
    }

    @Override
    public ResultVo<?> getSettingValue(String settingKey) {
        String settingValue = this.getOne(new QueryWrapper<Setting>().lambda().eq(Setting::getSettingKey, settingKey)).getSettingValue();
        return ResultVo.success(settingValue);
    }


}